<?php

namespace app\api\controller;

use think\Controller;
use think\Db;
use app\api\model\News as news_list;
use app\api\model\Preg;
use app\api\model\Talent as talent_list;
use app\api\model\Oss;

class Video extends Index
{
    public function management()
    {
        $number = (isset($_GET['number']) && !empty($_GET['number'])) ? $_GET['number'] : 10;
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
        $search = (isset($_GET['search']) && $_GET['search']) ? trim($_GET['search']) : null;
        $logmin = (isset($_GET['logmin']) && $_GET['logmin']) ? strtotime($_GET['logmin']) : 0;
        $logmax = (isset($_GET['logmax']) && $_GET['logmax']) ? (strtotime($_GET['logmax']) + 24 * 60 * 60 - 1) : time();
        $status = (isset($_GET['status']) && $_GET['status']) ? $_GET['status'] : array('neq', 3);
        $types = (isset($_GET['type']) && $_GET['type']) ? $_GET['type'] : array('neq', 0);
        $episode = (isset($_GET['episode']) && $_GET['episode']) ? $_GET['episode'] : "";
        if (!empty($episode)) {
            $map['w.episode'] = array('neq', 0);
        }
//        $uid=(isset($_GET['uid']) && $_GET['uid']) ?$_GET['uid']:"";
//        $map['w.uid']=$uid;
        $map['w.type'] = $types;
        $map['w.title'] = array('like', "%$search%");
        $map['w.time'] = array(array('egt', $logmin), array('elt', $logmax), 'and');
        $map['w.status'] = $status;
        $alias = 'w';
        $talent_list = new news_list('video_list');
        $count = $talent_list->count($map, $alias);
        $type = 'zgh_video_type';
        $condition1 = 'w.type=t.tid';
        $data = $talent_list->news_list($type, $map, $number, $page, $condition1);
        //dump($count);dump($data);
        $res = model('Common')->page_data($count, $page, $number);
        $res['count'] = $count;
        $res['data'] = $data;
        if (isset($res) && !empty($res)) {
            $this->_jsonp('200', '视频剧集列表数据获取成功', $res);
        } else {
            $this->_jsonp('1000', '视频剧集列表数据获取失败', null);
        }
    }

    public function management_add()
    {
        $data['title'] = input('post.videolistName');
        $data['type'] = input('post.videoRole');
        $data['remark'] = input('post.remark');
        $data['uid'] = input('post.uid');
        $data['status'] = 2;
        $data['time'] = time();
        $news_list = new news_list();
        if (!empty($_FILES)) {
            $files = request()->file('img');
            $img = $news_list->upload($files, 'Images/Cover');
            $data['pic'] = $img;
        }
        $talent_list = new talent_list('video_list');
        $res = $talent_list->talent_add($data);
        if (isset($res) && !empty($res)) {
            action_log('视频集添加', 'log_admin',1);
            $this->json('200', '添加成功', $res);
        } else {
            action_log('视频集添加', 'log_admin');
            $this->json('1000', '添加失败', null);
        }
    }

    public function management_edit()
    {
        $where['id'] = input('post.list_id');
        $data['title'] = input('post.videolistName');
        $data['type'] = input('post.videoRole');
        $data['remark'] = input('post.remark');
        $data['uid'] = input('post.uid');
        $news_list = new news_list();
        if (!empty($_FILES['img'])) {
            $files = request()->file('img');
            $img = $news_list->upload($files, 'Images/Cover');
            $data['pic'] = $img;
        }
        $talent_list = new talent_list('video_list');
        $res = $talent_list->talent_edit($data, $where);
        if (isset($res) && !empty($res)) {
            action_log('视频集编辑', 'log_admin',1);
            $this->json('200', '修改成功', $res);
        } else {
            action_log('视频集编辑', 'log_admin');
            $this->json('1000', '修改失败', null);
        }
    }

    public function management_detail()
    {
        $id = input('get.id');
        $talent_list = new news_list('video_list');
        $res = $talent_list->news_detail($id);
        if (isset($res) && !empty($res)) {
            $this->_jsonp('200', '查询成功', $res);
        } else {
            $this->_jsonp('1000', '查询失败', null);
        }
    }

    public function management_del()
    {
        $true = auth();
        if ($true == 2) {
            $id = input('get.id');
            $data['status'] = input('get.status');
            $news_list = new news_list('video_list');
//        $map['l_id']=$id;
//        $map['status']=array('neq',3);
            $res = $news_list->news_del($id, $data);
            if ($res) {
                action_log('视频集修改状态', 'log_admin',1);
                $this->_jsonp('200', $res['name'] . '成功', null);
            } else {
                action_log('视频集修改状态', 'log_admin');
                $this->_jsonp('1000', $res['name'] . '失败', null);
            }
        } else if($true == 1) {
            $this->_jsonp('1000', '您没有权限', null);
        }else{
			$this->_jsonp('1000','您未登录',null);
		}
    }

    /**
     * 异步上传视频
     *
     * @param OssClient $ossClient OSSClient实例
     * @param string $bucket bucket名字
     * @return
     */
    public function video_upload()
    {
        if (!empty($_FILES)) {
            $ext = substr($_FILES['file']['name'], stripos($_FILES['file']['name'], '.'));
            $rand = md5(time() . rand(100, 999));
            $object = "uploads/" . $rand . $ext;
            $ossClient = Oss::getInstance();
            $bucket = Oss::getBucketName();
            $content = $_FILES['file']['tmp_name'];
            $result = $ossClient->uploadFile($bucket, $object, $content);
            if ($result['oss-request-url']) {
                $this->json('200', '上传成功', $result['oss-request-url']);
            } else {
                $this->json('1000', '上传失败', null);
            }
        }
    }

    /**
     * 判断object是否存在
     *
     * @param OssClient $ossClient OSSClient实例
     * @param string $bucket bucket名字
     * @return null
     */
    function doesObjectExist()
    {
        $ossClient = Oss::getInstance();
        $bucket = Oss::getBucketName();
        $object = "uploads/4228fde4b042c7910eb29526888625c8..mp4";
        try {
            $exist = $ossClient->doesObjectExist($bucket, $object);
        } catch (OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }
        print(__FUNCTION__ . ": OK" . "\n");
        var_dump($exist);
    }

    /**
     * 删除object
     *
     * @param OssClient $ossClient OSSClient实例
     * @param string $bucket bucket名字
     * @return null
     */
    function deleteObject()
    {
        $ossClient = Oss::getInstance();
        $bucket = Oss::getBucketName();
        $object = "uploads/ceac2825ed645948771cce96c70543ee.mp4";
        try {
            $result = $ossClient->deleteObject($bucket, $object);
            dump($result);
        } catch (OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }
    }


    function video_add()
    {
        $data['l_id'] = input('post.l_id');
        $data['status'] = input('post.status');
        $data['content'] = input('post.content');
        $data['v_title'] = input('post.videoname');
        $data['v_abstract'] = input('post.abstract');
        $data['time'] = time();
//        $news_list=new news_list();
//        $arr=array();
//        if(!empty($_FILES)){
//            $files=request()->file('img');
//            $img=$news_list->upload($files,'Images/Cover');
//            $arr['pic']=$img;
//        }
        //      $result=array_merge($data,$arr);
        $talent_list = new talent_list('video');
        $news_list = new news_list('video_list');
        $where["id"] = $data['l_id'];
        $status = 0;
        Db::startTrans();
        try {
            $talent_list->talent_add($data);
            $news_list->plus($where, 'episode');
            // 提交事务
            Db::commit();
            $status = 1;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
        }
        if ($status == 1) {
            action_log('上传视频', 'log_admin',1);
            $this->json('200', '添加成功', null);
        } else {
            action_log('上传视频', 'log_admin');
            $this->json('1000', '添加失败', null);
        }
    }

    public function video_edit()
    {
        $where['id'] = input('post.id');
        $content = input('post.content');
        if (!empty($content)) {
            $data['content'] = input('post.content');
        }
//        $news_list=new news_list();
//        if(!empty($_FILES['img'])){
//            $files=request()->file('img');
//            $img=$news_list->upload($files,'Images/Cover');
//            $data['pic']=$img;
//        }
        $data['v_title'] = input('post.videoname');
        $data['v_abstract'] = input('post.abstract');
        //$data['file']=$_FILES;
        $talent_list = new talent_list('video');
        $res = $talent_list->talent_edit($data, $where);
        if ($res && !empty($res)) {
            action_log('修改视频成功', 'log_admin',1);
            $this->json('200', '修改成功', $data);
        } else {
            action_log('修改视频失败', 'log_admin');
            $this->json('200', '修改失败', null);
        }
    }

    public function video_list()
    {
        $search = (isset($_GET['search']) && $_GET['search']) ? trim($_GET['search']) : null;
        $logmin = (isset($_GET['logmin']) && $_GET['logmin']) ? strtotime($_GET['logmin']) : 0;
        $logmax = (isset($_GET['logmax']) && $_GET['logmax']) ? (strtotime($_GET['logmax']) + 24 * 60 * 60 - 1) : time();
        $status = (isset($_GET['status']) && $_GET['status']) ? $_GET['status'] : array('neq', 3);
        $l_id = (isset($_GET['l_id']) && $_GET['l_id']) ? $_GET['l_id'] : array('neq', 0);
        $map['w.l_id'] = $l_id;
        $map['w.v_title'] = array('like', "%$search%");
        $map['w.time'] = array(array('egt', $logmin), array('elt', $logmax), 'and');
        $map['w.status'] = $status;
        $talent_list = new talent_list('video');
        $type1 = 'zgh_video_list l';
        $condition1 = 'w.l_id=l.id';
        $type2 = 'zgh_video_type t';
        $condition2 = 'l.type=t.tid';
        $type3 = 'zgh_user u';
        $condition3 = 'l.uid=u.uid';
        $fields = "w.*,t.name,u.username,l.episode";
        $order = "w.kid asc";
        $data = $talent_list->video_list($type1, $type2, $type3, $map, $condition1, $condition2, $condition3, $fields, $order);
        if (isset($data) && !empty($data)) {
            $this->_jsonp('200', '视频剧集列表数据获取成功', $data);
        } else {
            $this->_jsonp('1000', '视频剧集列表数据获取失败', null);
        }
    }

    public function video_detail()
    {
        $map['w.id'] = input('get.id');
        //$map['w.status']=1;
        $talent_list = new talent_list('video');
        $type1 = 'zgh_video_list l';
        $condition1 = 'w.l_id=l.id';
        $type2 = 'zgh_video_type t';
        $condition2 = 'l.type=t.tid';
        $type3 = 'zgh_user u';
        $condition3 = 'l.uid=u.uid';
        $fields = "w.*,t.name,u.username,l.episode,l.title,l.remark";
        $data = $talent_list->video_detail($type1, $type2, $type3, $map, $condition1, $condition2, $condition3, $fields);
        if (isset($data) && !empty($data)) {
            $this->_jsonp('200', '视频详情获取成功', $data);
        } else {
            $this->_jsonp('1000', '视频详情获取失败', null);
        }
    }

    public function video_del()
    {
		 $true = auth();
		if ($true == 2) {
			$id = input('get.id');
			$pieces = explode(",", $id);
			$num = count($pieces);
			$data['status'] = input('get.status');
			$news_list = new news_list('video');
			$list = new news_list('video_list');
			$result = $news_list->news_detail($id);
			$where['id'] = $result['l_id'];
			$status = 0;
			Db::startTrans();
			try {
				if ($data['status'] == 1) {
					$list->plus($where, 'episode');
				} else {
					$list->minus($where, 'episode', $num);
				}
				$data = $news_list->news_del($id, $data);
				// 提交事务
				Db::commit();
				$status = 1;
			} catch (\Exception $e) {
				// 回滚事务
				Db::rollback();
			}
			if ($status) {
				action_log('删除视频', 'log_admin',1);
				$this->_jsonp('200', $data['name'] . '成功', null);
			} else {
				action_log('删除视频', 'log_admin');
				$this->_jsonp('1000', $data['name'] . '失败', null);
			}
		} else if($true == 1) {
            $this->_jsonp('1000', '您没有权限', null);
        }else{
			$this->_jsonp('1000','您未登录',null);
		}
    }

    //视频排序
    public function video_sort()
    {
        $arr = input('get.arr');
        $array = explode(',', $arr);
        $talent_list = new talent_list('video');
        foreach ($array as $key => $value) {
            $where['id'] = $value;
            $data['kid'] = $key;
            $talent_list->talent_edit($data, $where);
        }
    }

    //视频评论列表
    public function video_comment()
    {
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
        $number = 10;
        $id = input('get.id');
        $where['b.v_id'] = $id;
        $where['b.status'] = 1;
        $alias = 'b';
        $type1 = '__USER__ u';
        $condition1 = 'u.uid=b.uid';
        $fields = 'b.*,u.username,u.head_img';
        $news_list = new news_list('comment');
        $count = $news_list->count($where, $alias);
        $data = $news_list->two_list($where, $type1, $condition1, $number, $page, $fields);
		//屏蔽关键字
		$match = new Preg();
		foreach($data as $key=>$val){
                $html=$match->replace($val['comment']);
				$data[$key]['comment']=$html;
            }
			//
        $res = model('Common')->page_data($count, $page, $number);
        $Page = $news_list->page($count, $number);
        $render = $Page->render;
        $res['render'] = $render;
        $res['count'] = $count;
        $res['data'] = $data;
        if (isset($data) && !empty($data)) {
            $this->_jsonp('200', '视频评论列表数据获取成功', $res);
        } else {
            $this->_jsonp('1000', '视频评论列表数据获取失败', null);
        }
    }

    /*
    *视频评论删除
    */
    public function comment_del()
    {
        $id = input('get.id');
        $data['status'] = input('get.status');
        $news_list = new news_list('comment');
        $list = new news_list('video');
        $result = $news_list->news_detail($id);
        $where['id'] = $result['v_id'];
        $status = 0;
        Db::startTrans();
        try {
            $list->minus($where, 'comments');
            $res = $news_list->news_del($id, $data);
            // 提交事务
            Db::commit();
            $status = 1;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
        }
        if ($status) {
            action_log('删除视频评论', 'log_admin',1);
            $this->_jsonp('200', '删除成功', null);
        } else {
            action_log('删除视频评论', 'log_admin');
            $this->_jsonp('1000', '删除失败', null);
        }
    }

    /*
    *视频评论添加
    */
    public function comment_add()
    {
        $data['comment'] = htmlspecialchars(trim(input('get.comment')));
        $data['v_id'] = input('get.id');
        $where['id'] = input('get.id');
        $data['uid'] = input('get.uid');
        $data['status'] = 1;
        $data['time'] = time();
        $news_list = new news_list('video');
        $talent_list = new talent_list('comment');
        $status = 0;
        Db::startTrans();
        try {
            $news_list->plus($where, 'comments');
            $res = $talent_list->talent_add($data);
            // 提交事务
            Db::commit();
            $status = 1;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
        }
        if ($status) {
            action_log('视频评论添加', 'log_admin',1);
            $this->_jsonp('200', '添加评论成功', null);
        } else {
            action_log('视频评论添加失败', 'log_admin');
            $this->_jsonp('1000', '添加评论失败', null);
        }
    }

    /*
    *前台视频列表
    */
    public function course_list()
    {
        $number = (isset($_GET['number']) && $_GET['number']) ? trim($_GET['number']) : 10;
        $search = (isset($_GET['search']) && $_GET['search']) ? trim($_GET['search']) : null;
        $type = (isset($_GET['type']) && $_GET['type']) ? $_GET['type'] : array('neq', 0);
        $page = (isset($_GET['page']) && $_GET['page']) ? $_GET['page'] : 1;
        $order = (isset($_GET['order']) && $_GET['order']) ? $_GET['order'] : 0;
        switch ($order) {
            case 1:
                $sort = "w.time desc";
                break;
            case 2:
                $sort = "w.views desc";
                break;
            case 3:
                $sort = "rand()";
                break;
            default:
                $sort = "w.time desc,w.views desc";
        }
        $map['w.type'] = $type;
        $map['w.title'] = array('like', "%$search%");
        $map['w.status'] = 1;
        $map['w.episode'] = array('neq', 0);
        $alias = 'w';
        $talent_list = new news_list('video_list');
        $count = $talent_list->count($map, $alias);
        $type = 'zgh_video_type';
        $condition1 = 'w.type=t.tid';
        $data = $talent_list->news_list($type, $map, $number, $page, $condition1, $sort);
        //dump($count);dump($data);
        $res = model('Common')->page_data($count, $page, $number);
        $Page = $talent_list->page($count, $number);
        $render = $Page->render;
        $res['render'] = $render;
        $res['count'] = $count;
        $res['data'] = $data;
        if (isset($data) && !empty($data)) {
            $this->_jsonp('200', '视频剧集列表数据获取成功', $res);
        } else {
            $this->_jsonp('1000', '视频剧集列表数据获取失败', null);
        }
    }

    /*
    *前台视频点赞
    */
    public function likes()
    {
        $id = input('get.id');//视频id
        $uid = input('get.uid');
        $where['mid']=$id;
        $where['static']=1;//表示点赞
        $where['uid']=$uid;
        $arr=Db::name('likes')->where($where)->find();
        if (!empty($arr)) {
            $data1['static']=2;
            $map1['id']=$arr['id'];
            $map2['id']=$id;
            $map3['id']=Db::name('video')->where($map2)->value('l_id');
            $status = 0;
            Db::startTrans();
            try {
                Db::name('likes')->where($map1)->update($data1);
                Db::name('video')->where($map2)->setDec('v_likes');
                Db::name('video_list')->where($map3)->setDec('likes');
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
            }
            if ($status == 1) {
                $this->_jsonp('201', '取消点赞成功', null);
            } else {
                $this->_jsonp('1000', '取消点赞失败', null);
            }
        } else {
            $where['type']=1;
            $where['time']=time();
            $map2['id']=$id;
            $map3['id']=Db::name('video')->where($map2)->value('l_id');
            $status = 0;
            Db::startTrans();
            try {
                Db::name('likes')->insert($where);
                Db::name('video')->where($map2)->setInc('v_likes');
                Db::name('video_list')->where($map3)->setInc('likes');
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
            }
            //讲点赞用户写入
            if ($status == 1) {
                $this->_jsonp('200', '点赞成功', null);
            } else {
                $this->_jsonp('1000', '点赞失败', null);
            }
        }
    }
	 /*
   * 视频是否点赞
   */
    public function user_likes(){
        $where['mid'] = input('get.id');
        $where['type'] = input('get.type');
        $where['uid'] = input('get.uid');
        $where['static']=1;
        $res=Db::name('likes')->where($where)->find();
        if ($res) {
            $this->_jsonp('200', '点赞数据查询成功', $res);
        } else {
            $this->_jsonp('1000', '取消数据查询失败', null);
        }
    }

    /*
   * 视频收藏
   */
    public function collect()
    {
        $where['mid'] = input('get.id');//收藏的id
        $where['uid'] = input('get.uid');
        $where['type'] = 4;
        $where['static'] = 0;
        $base = new news_list('collect');
        $like = $base->check_name($where);
        $map['id'] = input('get.id');
        $list = new news_list('video');
        $l_vid = $list->check_name($map, 'l_id');
        $l_id['id'] = $l_vid['l_id'];
        //dump($arr);
        if ($like) {
            $where1['id']=$like['id'];
            $data1['static']=1;
            $status = 0;
            Db::startTrans();
            try {
                $like_add = Db::name("collect")->where($where1)->update($data1);
                $add = $list->minus($map, 'v_collect');
                Db::name('video_list')->where($l_id)->setDec('collect');
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
            }
            //dump($data);die;

            if ($status == 1) {
                $this->_jsonp('200', '取消收藏成功', null);
            } else {
                $this->_jsonp('1000', '取消收藏失败', null);
            }
        } else {
            $where['time']=time();
            // dump($data);die;
            //收藏用户写入
            $status = 0;
            Db::startTrans();
            try {
                $like_add = Db::name("collect")->insert($where);
                $add = $list->plus($map, 'v_collect');
                Db::name('video_list')->where($l_id)->setInc('collect');
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
            }

            if ($status == 1) {
                $this->_jsonp('201', '视频收藏成功', null);
            } else {
                $this->_jsonp('1000', '视频收藏失败', null);
            }
        }
    }

    /*
     * 視頻的浏览
     * */
    public function views()
    {
        $where['id'] = input('get.id');
        $basename = input('get.sheet');
        $view = (isset($_GET['views']) && $_GET['views']) ? trim($_GET['views']) : "views";
        $news_list = new news_list($basename);
        $l_vid = $news_list->check_name($where, 'l_id');
        $l_id['id'] = $l_vid['l_id'];
        $list = new news_list('video_list');
        $data = $news_list->plus($where, $view);
        $list->plus($l_id, 'views');
    }
}